#include "http_server.h"
#include <iostream>
#include <cassert>
#include <sstream>

// 定义友元测试类，专门用于访问HttpServer的私有成员
class HttpServerTest {
public:
// 测试HTTP请求解析功能
    static void test_request_parsing() {
        std::cout << "Testing request parsing... ";
        
        // 测试GET请求
        std::string get_request = "GET /index.html HTTP/1.1\r\n"
                                "Host: localhost:8080\r\n"
                                "Connection: keep-alive\r\n"
                                "\r\n";
        
        HttpRequest request;
        HttpServer server(8081);  // 使用不同端口避免冲突
        assert(server.parse_request(get_request, request) == true);
        assert(request.method == GET);
        assert(request.path == "/index.html");
        assert(request.version == "HTTP/1.1");
        assert(request.headers["Host"] == "localhost:8080");
        assert(request.headers["Connection"] == "keep-alive");
        
        // 测试POST请求
        std::string post_request = "POST /submit HTTP/1.1\r\n"
                                "Host: localhost:8080\r\n"
                                "Content-Length: 11\r\n"
                                "\r\n"
                                "test=123";
        
        assert(server.parse_request(post_request, request) == true);
        assert(request.method == POST);
        assert(request.path == "/submit");
        assert(request.body == "test=123");
        
        std::cout << "Passed!" << std::endl;
    }

    // 测试MIME类型获取功能
    static void test_mime_types() {
        std::cout << "Testing MIME types... ";
        
        HttpServer server(8082);
        assert(server.get_mime_type("html") == "text/html");
        assert(server.get_mime_type("css") == "text/css");
        assert(server.get_mime_type("js") == "application/javascript");
        assert(server.get_mime_type("jpg") == "image/jpeg");
        assert(server.get_mime_type("png") == "image/png");
        assert(server.get_mime_type("gif") == "image/gif");
        assert(server.get_mime_type("txt") == "application/octet-stream");
        
        std::cout << "Passed!" << std::endl;
    }

    // 测试错误响应创建功能
    static void test_error_responses() {
        std::cout << "Testing error responses... ";
        
        HttpServer server(8083);
        
        HttpResponse response = server.create_error_response(404, "Not Found");
        assert(response.status_code == 404);
        assert(response.status_msg == "Not Found");
        assert(response.headers["Content-Type"] == "text/html");
        assert(response.headers["Connection"] == "close");
        assert(response.body.find("404 Not Found") != std::string::npos);
        
        response = server.create_error_response(400, "Bad Request");
        assert(response.status_code == 400);
        assert(response.body.find("400 Bad Request") != std::string::npos);
        
        std::cout << "Passed!" << std::endl;
    }
};

// 主测试函数
int main() {
    HttpServerTest::test_request_parsing();
    HttpServerTest::test_mime_types();
    HttpServerTest::test_error_responses();
    std::cout << "All tests passed!" << std::endl;
    return 0;
}
    